package br.com.news.database.services;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import br.com.news.database.daos.interfaces.INoticiaDao;
import br.com.news.database.entities.NoticiaEntity;
import br.com.news.database.exception.DataBaseException;
import br.com.news.database.utils.GenericService;
import br.com.news.database.utils.interfaces.IGenericDao;
import br.com.news.utils.database.beans.NoticiaBean;
import br.com.news.utils.database.exception.DataBaseServiceException;
import br.com.news.utils.database.interfaces.INoticiaServiceDb;

@Named
public class NoticiaServiceDb extends GenericService<NoticiaBean, NoticiaEntity, Long> implements INoticiaServiceDb{

	@Inject
	private INoticiaDao noticiaDao;

	@Override
	protected IGenericDao<NoticiaEntity, Long> getConcreteDao() {
		return this.noticiaDao;
	}

	@Override
	public Long save(NoticiaBean noticiaBean) throws DataBaseServiceException {
		try {
			return noticiaDao.save(parserBean(noticiaBean));
		} catch (DataBaseException e) {
			throw new DataBaseServiceException("Erro ao tentar salvar a entidade",e);
		}
	}

	public boolean existeNoticia(NoticiaBean noticiaBean) {
		return noticiaDao.existeNoticia(parserBean(noticiaBean));
	}

	@Override
	public List<NoticiaBean> getUltimasNoticias(int qntNoticias,
			long idCategoria) throws DataBaseServiceException{
		try {
			return parserEntity(this.noticiaDao.getUltimasNoticias(qntNoticias, idCategoria));
		} catch (DataBaseException e) {
			throw new DataBaseServiceException("Erro ao tentar salvar a entidade",e);
		}
	}

	@Override
	public List<NoticiaBean> getNoticiasAnteriores(int qntNoticias,
			long idNoticia, long idCategoria) throws DataBaseServiceException{
		try {
			return parserEntity(this.noticiaDao.getNoticiasAnteriores(qntNoticias, idNoticia, idCategoria));
		} catch (DataBaseException e) {
			throw new DataBaseServiceException("Erro ao tentar salvar a entidade",e);
		}
	}

	@Override
	public List<NoticiaBean> findLastNews(long idNoticia, long idCategoria) throws DataBaseServiceException{
		try {
			return parserEntity(this.noticiaDao.findLastNews(idNoticia, idCategoria));
		} catch (DataBaseException e) {
			throw new DataBaseServiceException("Erro ao tentar salvar a entidade",e);
		}
	}
	
	@Override
	public NoticiaBean findNoticiaById(Long id)throws DataBaseServiceException  {
		try {
			return parserEntity(noticiaDao.findById(id));
		} catch (DataBaseException e) {
			throw new DataBaseServiceException("Erro ao buscar a noticia",e);
		}
	}
	
	@Override
	public Long quantidadePaginas(long idCategoria) {
		return noticiaDao.quantidadePaginas(idCategoria);
	}
	
	@Override
	public List<NoticiaBean> findNoticiasPorPagina(int linha,Long idCategoria,int quantidade) {
		return parserEntity(noticiaDao.findNoticiasPorPagina(linha, idCategoria,quantidade));
	}

	@Override
	public List<NoticiaBean> getNoticiasAnterioresXml(int qntNoticias,
			long idNoticia) throws DataBaseServiceException {
		try {
			return parserEntity(this.noticiaDao.getNoticiasAnterioresXml(qntNoticias, idNoticia));
		} catch (DataBaseException e) {
			throw new DataBaseServiceException("Erro ao tentar buscar últimas notícias",e);
		}
	}

	@Override
	public List<NoticiaBean> getUltimasNoticiasXml(int qntNoticias)
			throws DataBaseServiceException {
		try {
			return parserEntity(this.noticiaDao.getUltimasNoticiasXml(qntNoticias));
		} catch (DataBaseException e) {
			throw new DataBaseServiceException("Erro ao tentar buscar últimas notícias",e);
		}
	}
	
	@Override
	public NoticiaBean findImageByIdNoticia(Long idNoticia) throws DataBaseServiceException {
		try {
			NoticiaEntity noticiaEntity = this.noticiaDao.findImageByIdNoticia(idNoticia);
			if (noticiaEntity == null)
				throw new DataBaseServiceException("Não foi encontrato noticias para o id: "+ idNoticia);
					
			return parserEntity(noticiaEntity);
		} catch (DataBaseException e) {
			throw new DataBaseServiceException("Erro ao tentar buscar imagem para a notícia",e);
		}
	}
	
	@Override
	public List<NoticiaBean> buscaNoticiaPorNome(String nome) {
		return parserEntity(noticiaDao.buscaNoticiaPorNome(nome));
	}

	@Override
	public List<NoticiaBean> findByExample(NoticiaBean noticiaBean) {
		return parserEntity(noticiaDao.findByExample(parserBean(noticiaBean)));
	}


}
